package com.pig4cloud.pig.module.controller.survey;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.estate.Estate;
import com.pig4cloud.pig.module.api.entity.property.Property;
import com.pig4cloud.pig.module.api.entity.survey.SurveyUserRecord;
import com.pig4cloud.pig.module.service.estate.IEstateService;
import com.pig4cloud.pig.module.service.property.IPropertyService;
import com.pig4cloud.pig.module.service.survey.ISurveyQuestionService;
import com.pig4cloud.pig.module.service.survey.ISurveyService;
import com.pig4cloud.pig.module.service.survey.ISurveyUserRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 问卷调查的用户填报数据Controller
 *
 * @author ruoyi
 * @date 2021-09-05
 */
@Validated
@Api(value = "问卷调查的用户填报数据控制器", tags = {"问卷调查的用户填报数据管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/survey/record")
public class SurveyUserRecordController {

    private final ISurveyService surveyService;
    private final ISurveyQuestionService surveyQuestionService;
    private final ISurveyUserRecordService surveyUserRecordService;

	private final IPropertyService propertyService;
	private final IEstateService estateService;
    /**
     * 查询问卷调查的用户填报数据列表
     */
    @ApiOperation("查询问卷调查的用户填报数据列表")
    @PreAuthorize("@pms.hasPermission('module:survey_record:list')")
    @GetMapping("/list")
    public TableDataInfo<SurveyUserRecord> list(@Validated SurveyUserRecord surveyUserRecord) {
        QueryWrapper<SurveyUserRecord> qw = new QueryWrapper<>();
        if(surveyUserRecord.getSurveyId()!=null){
            qw.eq("survey_id",surveyUserRecord.getSurveyId());
        }
        if(StrUtil.isNotEmpty(surveyUserRecord.getPhoneNum())){
            qw.eq("phone_num",surveyUserRecord.getPhoneNum());
        }
        TableDataInfo<SurveyUserRecord> td = PageUtils.buildDataInfo(surveyUserRecordService.page(PageUtils.buildPage(),qw));
        List<SurveyUserRecord> list = td.getData();
        list.forEach(record->{

			Estate estate = estateService.findById(record.getEstateId());
			if(estate!=null){
				record.setEstateName(estate.getEstateName());
			}

			Property property = propertyService.findById(record.getPropertyId());
			if(property!=null){
				record.setPropertyName(property.getPropertyName());
			}
        });
        return td;
    }

    /**
     * 导出问卷调查的用户填报数据列表
     */
    @ApiOperation("导出问卷调查的用户填报数据列表")
    @PreAuthorize("@pms.hasPermission('module:survey_record:export')")
    @SysLog("问卷调查的用户填报数据")
    @GetMapping("/export")
    public List export(@Validated SurveyUserRecord surveyUserRecord) {
        List<SurveyUserRecord> list = surveyUserRecordService.list();
        // ExcelUtil<SurveyUserRecord> util = new // ExcelUtil<SurveyUserRecord>(SurveyUserRecord.class);
        return  list;// util.exportExcel(list, "问卷调查的用户填报数据");
    }

    /**
     * 获取问卷调查的用户填报数据详细信息
     */
    @ApiOperation("获取问卷调查的用户填报数据详细信息")
    @PreAuthorize("@pms.hasPermission('module:survey_record:query')")
    @GetMapping("/{id}")
    public R<SurveyUserRecord> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        SurveyUserRecord surveyUserRecord = surveyUserRecordService.getById(id);
        return R.ok(surveyUserRecord);
    }

    /**
     * 新增问卷调查的用户填报数据
     */
    @ApiOperation("新增问卷调查的用户填报数据")
    @PreAuthorize("@pms.hasPermission('module:survey_record:add')")
    @SysLog("问卷调查的用户填报数据")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody SurveyUserRecord surveyUserRecord) {
        return R.okByRow(surveyUserRecordService.save(surveyUserRecord) ? 1 : 0);
    }

    /**
     * 修改问卷调查的用户填报数据
     */
    @ApiOperation("修改问卷调查的用户填报数据")
    @PreAuthorize("@pms.hasPermission('module:survey_record:edit')")
    @SysLog("问卷调查的用户填报数据")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody SurveyUserRecord surveyUserRecord) {
        return R.okByRow(surveyUserRecordService.updateById(surveyUserRecord) ? 1 : 0);
    }

    /**
     * 删除问卷调查的用户填报数据
     */
    @ApiOperation("删除问卷调查的用户填报数据")
    @PreAuthorize("@pms.hasPermission('module:survey_record:remove')")
    @SysLog("问卷调查的用户填报数据")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(surveyUserRecordService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
